﻿Imports Sunts.HyperV.Common


Public Class frmMailRecipient

    Public Property MailRecipient As MailRecipient

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            If Me.txtRecipientMailAddress.Text = String.Empty Or Me.txtRecipientName.Text = String.Empty Then
                MessageBox.Show("Could not add the mail recipient. Ensure the details have been entered")
                Exit Sub
            End If

            If MailRecipient Is Nothing Then
                MailRecipient = New MailRecipient
                MailRecipient.Recipient = txtRecipientName.Text
                MailRecipient.EmailAddress = txtRecipientMailAddress.Text
                MailRecipient.Insert(My.Settings.ConnectionString)
            Else
                MailRecipient.Recipient = txtRecipientName.Text
                MailRecipient.EmailAddress = txtRecipientMailAddress.Text
                MailRecipient.Update(My.Settings.ConnectionString)
            End If

            MessageBox.Show("Recipient details saved successfully")

            Me.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub EmailRecipient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If MailRecipient IsNot Nothing Then
            Dim MailRecipientCollection As MailRecipientCollection = MailRecipient.SelectAll(My.Settings.ConnectionString)
            For Each MailRecipient As MailRecipient In MailRecipientCollection
                txtRecipientName.Text = MailRecipient.Recipient
                txtRecipientMailAddress.Text = MailRecipient.EmailAddress
            Next
            MailRecipientCollection = Nothing
        End If
    End Sub

End Class